package com.zhiyou100.controller;

import com.zhiyou100.entity.User;
import com.zhiyou100.service.UserService;
import com.zhiyou100.util.PublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import sun.security.provider.MD5;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.security.MessageDigest;

@Controller
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private UserService userService;
    @RequestMapping("/login.action")
    public String loginMethod(String uyzm,User u, Model m, HttpSession session)throws Exception{
        System.out.println(uyzm+":::"+session.getAttribute("yzm"));
        if(!uyzm.equalsIgnoreCase((String) session.getAttribute("yzm"))){
            m.addAttribute("message","验证码错误!");
            return "user_login";
        }
        //获取web.xml中的盐  key
        String key=session.getServletContext().getInitParameter("key");
        User dbUser=userService.getOneByUname(u.getUname());
        if(dbUser==null){
            m.addAttribute("message","用户名不存在!");
            return "user_login";
        }
        //获取表单的密码的md5加密后的值
        String upwdMD4= PublicUtil.EncoderByMd5(u.getUpwd(),key);
        if(!dbUser.getUpwd().equals(upwdMD4)){
            //拿md5值与数据库比较
            m.addAttribute("message","密码错误!");
            return "user_login";
        }
        //添加激活判断
        if(dbUser.getUstatus()!=1){
            m.addAttribute("message","账号没有激活!");
            return "user_login";
        }
        session.setAttribute("user",dbUser);
        return "views/show";
    }
    @RequestMapping("/yzm.action")
    public String yzmMethod(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        String yzmStr=req.getSession().getServletContext().getInitParameter("yzmStr");
        //随机一个字符串
        String  yzm="";
        for (int i = 0; i <4 ; i++) {
            yzm+=yzmStr.charAt((int)(Math.random()*yzmStr.length()));
        }
        req.getSession().setAttribute("yzm",yzm);
        PublicUtil.getYZM(yzm,resp.getOutputStream());
        return null;
    }
    @RequestMapping("/regist.action")
    public String registMethod(@RequestParam("uphotoFile") MultipartFile uphotoFile, User u, HttpServletRequest req)throws Exception{
        System.out.println("额呵呵");
        //获取上传文件的名字
        String fileName=uphotoFile.getOriginalFilename();
        fileName=System.currentTimeMillis()+fileName;
        //获取imgs的位置
        String path=req.getSession().getServletContext().getRealPath("/imgs");
        uphotoFile.transferTo(new File(path,fileName));//实现文件的输出到目的位置
        u.setUphoto(fileName);//设置头像路径
        String key=req.getSession().getServletContext().getInitParameter("key");
        u.setUpwd(PublicUtil.EncoderByMd5(u.getUpwd(),key));//更改密码位md5
        userService.addOne(u);
        u=userService.getOneByUname(u.getUname());
        String  protol="http://";
        String  server=req.getHeader("Host");
        String projectPath=req.getContextPath();
        req.setAttribute("message","注册成功，请登陆！");
        String url=protol+server+projectPath+"/user/jiHuo.action?uid="+u.getUid();
        String message="<a  href='"+url+"'>请点击此超链接完成激活,进入登陆页面！</a>";
        PublicUtil.sendMessage(u.getUemail(),"注册ssh项目激活邮件",message);//发送邮件
        return "user_login";
    }
    //通过ajax判断用户名是否可用
    @RequestMapping("/ajaxPdUname.action")
    @ResponseBody
    public String ajaxPdUnameMethod(String uname)throws Exception{
        //判断user的用户名是否存在
        User dbUser=userService.getOneByUname(uname);
        if(dbUser!=null){
           return "disable";
        }
        return "enable";
    }
    //用户激活
    @RequestMapping("/jiHuo.action")
    public String jiHuoMethod(Integer uid,HttpServletRequest req)throws Exception{
            userService.jiHuo(uid);
            req.setAttribute("message","激活成功！");
            return "user_login";
    }
}
